package com.gggd.aopdemo.controller;

import com.gggd.aopdemo.anno.Desensitization;
import com.gggd.aopdemo.util.WebApiResult;
import com.gggd.aopdemo.vo.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author srx
 * @date 2021/12/25 13:31
 */
@RestController
@RequestMapping("/desensitizationDemo")
public class SysUserController {
    @GetMapping("desensitization")
    //需要加入两个值
    @Desensitization(DataClass = WebApiResult.class, field = "data")
    public WebApiResult<SysUser> check(@RequestBody SysUser u){
        //测试集合
        List<SysUser> list = new ArrayList<>();
        list.add(u);
        SysUser u2 = new SysUser();
        BeanUtils.copyProperties(u, u2);
        u2.setUid(2);
        list.add(u2);
        SysUser u3 = new SysUser();
        BeanUtils.copyProperties(u, u3);
        u3.setUid(3);
        list.add(u3);
        return WebApiResult.ok("成功", list);

        //测试对象
//        return WebApiResult.ok("成功", u);
    }

    @GetMapping("check1")
    //因为返回体是集合或者对象，所以不需要加field字段
    @Desensitization(DataClass = SysUser.class)
    public SysUser check1(@RequestBody SysUser u){
        return u;
    }

    @GetMapping("check2")
    //因为返回体是集合或者对象，所以不需要加field字段
    @Desensitization(DataClass = SysUser.class)
    public List<SysUser> check2(@RequestBody SysUser u){
        List<SysUser> list = new ArrayList<>();
        list.add(u);
        SysUser u2 = new SysUser();
        BeanUtils.copyProperties(u, u2);
        u2.setUid(2);
        list.add(u2);
        SysUser u3 = new SysUser();
        BeanUtils.copyProperties(u, u3);
        u3.setUid(4);
        list.add(u3);
        return list;
    }
}
